package com.example.demo.web.controller;

import java.util.List;

import com.example.demo.core.handler.exception.ResultBO;
import com.example.demo.core.handler.exception.ResultTool;

import com.example.demo.sys.service.bo.UserFullBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.sys.service.MenuService;
import com.example.demo.sys.service.UserService;
import com.example.demo.sys.service.bo.MenuBO;
import com.example.demo.sys.service.bo.UserBO;

@Controller
@RequestMapping("/sys")
public class SystemController {
	@Autowired
	private MenuService menuService;
	@Autowired
	private UserService userService;

	@GetMapping("/menu")
	@ResponseBody
	public ResultBO<?> getMenus() {
		List<MenuBO> tree = menuService.getAllMenus();
		return ResultTool.success(tree);
	}

	@PutMapping("/menu")
	@ResponseBody
	public ResultBO<?> updateMenu(@RequestBody MenuBO menu) {
		menuService.updateMenu(menu);
		return ResultTool.success(menu);
	}

	@PostMapping("/menu")
	@ResponseBody
	public ResultBO<?> addMenu(@RequestBody MenuBO menu) {
		menuService.addMenu(menu);
		return ResultTool.success(menu);
	}

	@DeleteMapping("/menu/{id}")
	@ResponseBody
	public ResultBO<?> removeMenu(@PathVariable(value = "id") Integer id) {
		menuService.removeMenu(id);
		return ResultTool.success(id);
	}

	@GetMapping("/users")
	@ResponseBody
	public ResultBO<?> getUsers() {
		List<UserFullBO> users = userService.getAllUsers();
		return ResultTool.success(users);
	}

}
